/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.turingtechnologies.materialscrollbardemo.adapters;

import com.turingtechnologies.materialscrollbardemo.MaterialScrollBar;
import com.turingtechnologies.materialscrollbardemo.ResourceTable;
import com.turingtechnologies.materialscrollbardemo.bean.ListBean;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.List;

public class DateViewCreateHelper {
    private AbilitySlice slice;

    /**
     * 创建实例
     *
     * @param abilitySlice 上下文
     */
    public DateViewCreateHelper(AbilitySlice abilitySlice) {
        this.slice = abilitySlice;
    }

    /**
     * 创建Component
     *
     * @param res 资源文件
     * @param position 位置
     * @return 返回Component
     */
    public Component createView(int res, int position) {
        Component mainComponent = LayoutScatter.getInstance(slice).parse(res, null, false);
        ComponentContainer rootLayout = (ComponentContainer) mainComponent;
        initViewDateActivity(rootLayout, position);
        return rootLayout;
    }

    /**
     * 初始化数据
     *
     * @param mainComponent 布局容器
     * @param position 位置
     */
    public void initViewDateActivity(ComponentContainer mainComponent, int position) {
        // 列表
        ListContainer listContainer = (ListContainer) mainComponent.findComponentById(ResourceTable.Id_list_con);
        MaterialScrollBar materiaScroller = (MaterialScrollBar) mainComponent.findComponentById(ResourceTable.Id_fast);
        List<ListBean> list = getData();

        SampleItemProvider itemPro = new SampleItemProvider(list, this.slice);
        listContainer.setItemProvider(itemPro);
        listContainer.setDraggedListener(Component.DRAG_HORIZONTAL_VERTICAL, null);
        materiaScroller.setListContainer(listContainer,2,0);
    }

    private ArrayList<ListBean> getData() {
        ArrayList<ListBean> list = new ArrayList<>();
        list.add(new ListBean("京东",ResourceTable.Media_one));
        list.add(new ListBean("今日头条",ResourceTable.Media_seven));
        list.add(new ListBean("58同城",ResourceTable.Media_four));
        list.add(new ListBean("58同城",ResourceTable.Media_five));
        list.add(new ListBean("58同城",ResourceTable.Media_six));
        list.add(new ListBean("图库",ResourceTable.Media_two));
        list.add(new ListBean("设置",ResourceTable.Media_three));
        list.add(new ListBean("UC 浏览器",ResourceTable.Media_seven));
        list.add(new ListBean("WPS Office",ResourceTable.Media_eight));
        list.add(new ListBean("主题",ResourceTable.Media_night));
        list.add(new ListBean("云空间",ResourceTable.Media_ten));
        return list;
    }
}
